package org.me.Slide;

import java.util.List;

import org.me.Slide.Adapter.AlbumGridAdapter;
import org.me.Slide.Util.GlobalVariable;
import org.me.Slide.Util.XMLUtil;
import org.me.Slide.data.Albums;





import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumGridActivity extends Activity {
	
	private Bundle extra;
	private GridView grid;
	private Context mContext;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.album_gird_activity);
        
        extra = getIntent().getExtras();
        mContext = this;
        setGridViewNumColums();
        TextView txtTile = (TextView)findViewById(R.id.albums_activity_txt_title);
        ImageView imageView = (ImageView)findViewById(R.id.albums_activity_image_view_grid_view);
        TextView btnBack = (TextView)findViewById(R.id.TextView);
        btnBack.setText(getResources().getString(R.string.txtAlbum));
        btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				
			}
		});
        grid = (GridView)findViewById(R.id.albums_activity_grid_view);
        txtTile.setText(extra.getString("TITLE"));
        
        if(extra.getString("ID").equals("null"))
        {
        	Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nophotos);
        	grid.setVisibility(View.GONE);
        	imageView.setImageBitmap(bitmap);
        }
        else
        {
        	imageView.setVisibility(View.VISIBLE);
        	List<Albums> al = XMLUtil.parseDataFromXMLtoGridView(this, extra.getString("ID"));
        	Log.v("id", "id"+extra.getString("ID"));
        	AlbumGridAdapter adapter = new AlbumGridAdapter(this,  al,extra.getString("ID"));
        	grid.setAdapter(adapter);
        }
        
     
	}
	
	private void setGridViewNumColums()
    {
    	GlobalVariable.config = getResources().getConfiguration(); 
    	try
    	{
	        if(GlobalVariable.config.orientation == Configuration.ORIENTATION_PORTRAIT)
	        {
	        	//Màn hình dọc
	        	grid.setNumColumns(4);
	        } 
	        else if(GlobalVariable.config.orientation == Configuration.ORIENTATION_LANDSCAPE)
	        {
	        	//Màn hình ngang
	        	grid.setNumColumns(6);
	        }
    	}
    	catch (Exception e) {}
    }


}
